import { login, logout, ResetToken } from '@/api/user'

import { getToken, setToken, removeToken } from '@/utils/auth'
import router, { resetRouter } from '@/router'

const state = {
    token: getToken(),
    name: '',
    avatar: '',
    //introduction: '',
    //roles: ['admin'],
    menus: [],
    user_data: {},
    //admin:管理员,ent:用能企业,srg:服务机构,gov:政府机构,expert:专家
    account_type: 'undefined'
}

const getters = {


};
const mutations = {
    SET_TOKEN: (state, token) => {
        state.token = token
    },
    SET_MENU: (state, menu) => {
        state.menus = menu
    },
    SET_USERDATA: (state, user_data) => {
        state.user_data = user_data
        state.name = user_data.alias_name
    },
    SET_ACCOUNTTYPE: (state, account_type) => {
        state.account_type = 'undefined'
        if (account_type == 1) {
            state.account_type = 'admin'
        } else if (account_type == 2) {
            state.account_type = 'ent'
        } else if (account_type == 3) {
            state.account_type = 'srg'
        } else if (account_type == 4) {
            state.account_type = 'gov'
        } else if (account_type == 5) {
            state.account_type = 'expert'
        }
    },
    SET_NAME: (state, name) => {
        state.name = name
    },
    SET_AVATAR: (state, avatar) => {
        state.avatar = avatar
    }
}
import MD5 from 'md5.js'
const actions = {
    // user login
    login({ commit }, userInfo) {
        var post_pwd = new MD5().update(userInfo.pwd).digest('hex');
        var post_data = {
            account: userInfo.account,
            pwd: post_pwd,
            validate_code: userInfo.validate_code,
            validate_key: userInfo.validate_key,
            type: userInfo.type,
            code: userInfo.code,
        };
        return new Promise((resolve, reject) => {
            login(post_data).then(response => {
                const { data } = response
                if (response.code == 0) {
                    commit('SET_TOKEN', data.user_data.token)
                    commit('SET_NAME', data.user_data.alias_name)
                    commit('SET_MENU', data.menus)
                    commit('SET_USERDATA', data.user_data)
                    commit('SET_ACCOUNTTYPE', data.user_data.account_type)
                    setToken(data.user_data.token)
                }
                resolve(response);
            }).catch(error => {
                reject(error)
            })
        })
    },

    // get user info
    getInfo({ commit, state }) {
        return new Promise((resolve, reject) => {
            ResetToken({}).then(response => {
                const { data } = response
                if (response.code != 0) {
                    reject('getInfo: roles must be a non-null array!')
                }
                if (!data) {
                    reject('Verification failed, please Login again.')
                }
                commit('SET_TOKEN', data.user_data.token)
                commit('SET_NAME', data.user_data.alias_name)
                commit('SET_MENU', data.menus)
                commit('SET_USERDATA', data.user_data)
                commit('SET_ACCOUNTTYPE', data.user_data.account_type)
                setToken(data.user_data.token)
                resolve(data)
            }).catch(error => {
                reject(error)
            })

        })
    },

    // user logout
    logout({ commit, state, dispatch }) {
        return new Promise((resolve, reject) => {
            logout(state.token).then(() => {
                commit('SET_TOKEN', '') //清除Token
                commit('SET_ACCOUNTTYPE', -1) //清除清除账号类型
                removeToken()
                resetRouter()

                // reset visited views and cached views
                // to fixed https://github.com/PanJiaChen/vue-element-admin/issues/2485
                dispatch('tagsView/delAllViews', null, { root: true })

                resolve()
            }).catch(error => {
                reject(error)
            })
        })
    },

    // remove token
    resetToken({ commit }) {
        return new Promise(resolve => {
            commit('SET_TOKEN', '') //清除Token
            commit('SET_ACCOUNTTYPE', -1) //清除清除账号类型
            removeToken()
            resolve()
        })
    },

    // dynamically modify permissions
    async changeRoles({ commit, dispatch }, role) {

    }
}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}